<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: STL iterators and algorithms</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TutorialSTL.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">STL iterators and algorithms<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>Since the version 3.4, Eigen's dense matrices and arrays provide STL compatible iterators. As demonstrated below, this makes them naturally compatible with range-for-loops and STL's algorithms.</p>
<h1><a class="anchor" id="TutorialSTLVectors"></a>
Iterating over 1D arrays and vectors</h1>
<p>Any dense 1D expressions exposes the pair of <code>begin()/end()</code> methods to iterate over them.</p>
<p>This directly enables c++11 range for loops: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#gaf20e523ca57ee8ef0a945cd4703d2bfd">VectorXi</a> v = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">VectorXi::Random</a>(4);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Here is the vector v:\n&quot;</span>;</div>
<div class="line"><span class="keywordflow">for</span>(<span class="keyword">auto</span> x : v) cout &lt;&lt; x &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_ae814abb451b48ed872819192dc188c19"><div class="ttname"><a href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Eigen::DenseBase::Random</a></div><div class="ttdeci">static const RandomReturnType Random()</div><div class="ttdef"><b>Definition:</b> Random.h:114</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_gaf20e523ca57ee8ef0a945cd4703d2bfd"><div class="ttname"><a href="group__matrixtypedefs.html#gaf20e523ca57ee8ef0a945cd4703d2bfd">Eigen::VectorXi</a></div><div class="ttdeci">Matrix&lt; int, Dynamic, 1 &gt; VectorXi</div><div class="ttdoc">Dynamic×1 vector of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here is the vector v:
7 -2 6 6 
</pre> </td></tr>
</table>
<p>One dimensional expressions can also easily be passed to STL algorithms: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">Array4i v = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Array4i::Random</a>().abs();</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Here is the initial vector v:\n&quot;</span> &lt;&lt; v.transpose() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line">std::sort(v.begin(), v.end());</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Here is the sorted vector v:\n&quot;</span> &lt;&lt; v.transpose() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here is the initial vector v:
7 2 6 6
Here is the sorted vector v:
2 6 6 7
</pre> </td></tr>
</table>
<p>Similar to <code>std::vector</code>, 1D expressions also exposes the pair of <code>cbegin()/cend()</code> methods to conveniently get const iterators on non-const object.</p>
<h1><a class="anchor" id="TutorialSTLMatrices"></a>
Iterating over coefficients of 2D arrays and matrices</h1>
<p>STL iterators are intrinsically designed to iterate over 1D structures. This is why <code>begin()/end()</code> methods are disabled for 2D expressions. Iterating over all coefficients of a 2D expressions is still easily accomplished by creating a 1D linear view through <code>reshaped()</code>: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga757e178bb30b809f0aef66d97ced667a">Matrix2i</a> A = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Matrix2i::Random</a>();</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Here are the coeffs of the 2x2 matrix A:\n&quot;</span>;</div>
<div class="line"><span class="keywordflow">for</span>(<span class="keyword">auto</span> x : A.reshaped())</div>
<div class="line">  cout &lt;&lt; x &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga757e178bb30b809f0aef66d97ced667a"><div class="ttname"><a href="group__matrixtypedefs.html#ga757e178bb30b809f0aef66d97ced667a">Eigen::Matrix2i</a></div><div class="ttdeci">Matrix&lt; int, 2, 2 &gt; Matrix2i</div><div class="ttdoc">2×2 matrix of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here are the coeffs of the 2x2 matrix A:
7 -2 6 6 
</pre> </td></tr>
</table>
<h1><a class="anchor" id="TutorialSTLRowsColumns"></a>
Iterating over rows or columns of 2D arrays and matrices</h1>
<p>It is also possible to get iterators over rows or columns of 2D expressions. Those are available through the <code>rowwise()</code> and <code>colwise()</code> proxies. Here is an example sorting each row of a matrix: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">ArrayXXi A = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">ArrayXXi::Random</a>(4,4).abs();</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Here is the initial matrix A:\n&quot;</span> &lt;&lt; A &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><span class="keywordflow">for</span>(<span class="keyword">auto</span> row : A.rowwise())</div>
<div class="line">  std::sort(row.begin(), row.end());</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Here is the sorted matrix A:\n&quot;</span> &lt;&lt; A &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here is the initial matrix A:
7 9 5 3
2 6 1 0
6 3 0 9
6 6 3 9
Here is the sorted matrix A:
3 5 7 9
0 1 2 6
0 3 6 9
3 6 6 9
</pre> </td></tr>
</table>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
